﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class Message_Sender : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // string fromUserId = Session["username"].ToString();
            List<Message> msgList = new MessageManager().GetMessageByFromUserId("admin");
            this.GridView1.DataSource = msgList;
            this.GridView1.DataBind();
        }


    }
    public string GetToUserId(object id)
    {
        int msgId = Convert.ToInt32(id);
        string userName = null;
        List<MessageToUser> userList = new MsgToUserManager().GetAllMsgToUserByMsgId(msgId);
        if (userList.Count != 0)
        {
            foreach (MessageToUser item in userList)
            {
                userName = item.ToUserId;
                return userName;
            }
        }

        return null;


    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow item in GridView1.Rows)
        {
            if (item.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)(item.Cells[0].Controls[0].FindControl("cbDelete"));

                if (cb.Checked == true)
                {
                    int msgId = Convert.ToInt32(GridView1.DataKeys[item.RowIndex].Value);
                    new MessageManager().DeleteMessageByIdAndFromUserId(msgId, Session["username"].ToString());
                }
            }
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("PersonalMessage.aspx");
    }
    protected void cbAll_CheckedChanged(object sender, EventArgs e)
    {
        if (this.cbAll.Checked == true)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)item.Cells[0].FindControl("cbDelete");
                    cb.Checked = true;

                }
            }
        }
        else
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)item.Cells[0].FindControl("cbDelete");
                    cb.Checked = false;

                }
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "current=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=current");
            e.Row.Attributes.Add("onclick", "javascript:OpenMsg(300,300," + GridView1.DataKeys[e.Row.RowIndex].Value + ")");
        }
    }
}